package org.example.gift.service.impl;

import org.example.gift.dto.RegisterDto;
import org.example.gift.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import org.example.gift.client.LoginUserFeignClient;
import org.example.gift.domain.LoginUser;
import org.example.gift.domain.User;
import org.example.gift.domain.UserInfo;
import org.example.gift.dto.RegisterDto;
import org.example.gift.enums.SystemErrorCode;
import org.example.gift.mapper.UserInfoMapper;
import org.example.gift.mapper.UserMapper;
import org.example.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.UUID;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginUserFeignClient loginUserFeignClient;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public void userRegister(RegisterDto registerDto) {

        //组装登录对象
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setPassword(registerDto.getPassword());
        loginUser.setEmail(null);
        loginUser.setTel(registerDto.getPhone());
        loginUser.setType(LoginUser.USER_WEB);
        AjaxResult ajaxResult = loginUserFeignClient.addOrUpdate(loginUser);
        RYGAssert.isTrue(ajaxResult.getSuccess(),SystemErrorCode.RESPONSE_CODE_400500);
        //获取远程数据
        Long loginId =Long.valueOf(ajaxResult.getData().toString());


        //组装用户基本信息
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setPhone(registerDto.getPhone());
        user.setPassword(registerDto.getPassword());
        user.setSalt(null);
        user.setBitState(1L);
        user.setSecLevel(1);
        user.setLoginId(loginId);
        userMapper.insert(user);

        //组装用户详情信息
        UserInfo userInfo = new UserInfo();
        userInfo.setRegTime(new Date().getTime());
        userInfo.setLevel(0);
        userInfo.setGrowScore(0);
        userInfo.setUserId(user.getId());
        userInfoMapper.insert(userInfo);

    }
}